namespace Digitalmes.Application.Services.Impl;

/// <summary>
/// 系统配置 <see cref="SysConfig"/> 对象缓存。
/// </summary>
internal sealed class SysConfigQueryService(IMemoryCache memoryCache, IServiceProvider serviceProvider) : ISysConfigQueryService
{
    public async Task<string?> GetValueAsync(string code)
    {
        var configs = await GetListAsync();
        return configs.FirstOrDefault(s => s.Code == code)?.Value;
    }

    private async Task<IReadOnlyList<SysConfig>> GetListAsync()
    {
        return await memoryCache.GetOrCreateAsync(Cachekeys.SysConfigKey, _ =>
        {
            var repo = serviceProvider.GetRequiredService<IRepository<SysConfig>>();
            return repo.GetListAsync(m => m.Status == EnabledStatusEnum.Enabled);
        }) ?? [];
    }
}
